import Vue from 'vue'
import Router from 'vue-router'
import store from '../vuex'
import {clearvuex} from "../vuex/clear";
import {message,getLocalStorage,delLocalStorage} from '../common/util'

import IndexPage from '@/pages/index'
import LoginPage from '@/pages/login'
import SettingPage from '@/pages/setting'
import PasswordPage from '@/pages/password'
import SubscribeManagerPage from '@/pages/subscribeManager'
import AddSubscribePage from '@/pages/subscribeManager/addSubscribe'
import HolidayPartyListPage from '@/pages/subscribeManager/holidayPartyList'
import HolidayPartyDetailPage from '@/pages/subscribeManager/holidayPartyDetail'
import ToPayDepositPage from '@/pages/subscribeManager/toPayDeposit'
import SubscribeDetailPage from '@/pages/subscribeManager/subscribeDetail'
import DepositTermPage from '@/pages/subscribeManager/depositTerm'
import OrderDetailPage from '@/pages/subscribeManager/orderDetail'
import PresentManagerPage from '@/pages/presentManager'
import ChoosePresentPage from '@/pages/presentManager/choosePresent'
import SubmitPresentPage from '@/pages/presentManager/submitPresent'
import PresentedListPage from '@/pages/presentManager/presentedList'
import PresentDetailPage from '@/pages/presentManager/presentDetail'
import PresentStatisticsPage from '@/pages/presentManager/presentStatistics'

import TurnoverStatisticsPage from '@/pages/statistics/turnoverStatistics'
import TodaySelfHelpCustomerPage from '@/pages/statistics/todaySelfHelpCustomer'
import OperatingRevenuePage from '@/pages/statistics/operatingRevenue'
import TurnoverRatioPage from '@/pages/statistics/turnoverRatio'
import OpenRatioPage from '@/pages/statistics/openRatio'
import AreaTimeStatisticsPage from '@/pages/statistics/areaTimeStatistics'
import AreaMinChargeStatisticsPage from '@/pages/statistics/areaMinChargeStatistics'
import SalesSummaryPage from '@/pages/statistics/salesSummary'
import DanceRevenuePage from '@/pages/statistics/danceRevenue'
import CardRevenuePage from '@/pages/statistics/cardRevenue'
import OnAccountStatisticsPage from '@/pages/statistics/onAccountStatistics'
import PresentStatisticsDetailPage from '@/pages/statistics/presentStatisticsDetail'
import DamagedStatisticsPage from '@/pages/statistics/damagedStatistics'
import StockDataPage from '@/pages/statistics/stockData'

import SubscribePerformancePage from '@/pages/performance/subscribePerformance'
import DrinksPerformancePage from '@/pages/performance/drinksPerformance'
import OrderPerformancePage from '@/pages/performance/orderPerformance'
import DancePerformancePage from '@/pages/performance/dancePerformance'
import DrinksRankingPage from '@/pages/performance/drinksRanking'
import AreaRankingPage from '@/pages/performance/areaRanking'
import MemberRankingPage from '@/pages/performance/memberRanking'
import TableSalesRankingPage from '@/pages/performance/tableSalesRanking'
import TableSubscribeRankingPage from '@/pages/performance/tableSubscribeRanking'

Vue.use(Router)

const router = new Router({
  //mode:'history',//网址没有了#号，hBuilder打包成APP不能使用，需要注释
  base: '/boss/',
  routes: [
    {
      path: '/login',
      name: '登录',
      component: LoginPage
    },
    {
      path: '/password',
      name: '重置密码',
      component: PasswordPage
    },
    {
      path: '/',
      name: '主页',
      component: IndexPage,
      meta:{requireAuth: true }
    },
    {
      path: '/setting',
      name: '设置',
      component: SettingPage,
      meta:{requireAuth: true }
    },
    {
      path: '/subscribeManager',
      name: '客户预订管理',
      component: SubscribeManagerPage,
      meta:{requireAuth: true, module:['subscribeManager_boss'] }
    },
    {
      path: '/addSubscribe/:subscribe',
      name: '增加预订',
      component: AddSubscribePage,
      meta:{requireAuth: true, module:['subscribeManager_boss'] }
    },
    {
      path: '/holidayPartyList',
      name: '节日/派对列表',
      component: HolidayPartyListPage,
      meta:{requireAuth: true, module:['subscribeManager_boss'] }
    },
    {
      path: '/holidayPartyDetail/:holidayPartyId',
      name: '节日/派对详情',
      component: HolidayPartyDetailPage,
      meta:{requireAuth: true, module:['subscribeManager_boss'] }
    },
    {
      path: '/toPayDeposit/:subscribeId',
      name: '订金支付',
      component: ToPayDepositPage,
      meta:{requireAuth: true, module:['subscribeManager_boss'] }
    },
    {
      path: '/subscribeDetail/:subscribeId/:type',
      name: '预订详情',
      component: SubscribeDetailPage,
      meta:{requireAuth: true, module:['subscribeManager_boss'] }
    },
    {
      path: '/depositTerm',
      name: '预订协议',
      component: DepositTermPage,
      meta:{requireAuth: true, module:['subscribeManager_boss'] }
    },
    {
      path: '/orderDetail/:orderId',
      name: '订单详情',
      component: OrderDetailPage,
      meta:{requireAuth: true, module:['presentManager_boss','subscribeManager_boss'] }
    },
    {
      path: '/presentManager',
      name: '当前可赠送列表',
      component: PresentManagerPage,
      meta:{requireAuth: true, module:['presentManager_boss'] }
    },
    {
      path: '/choosePresent/:type/:orderId',
      name: '选择要赠送商品',
      component: ChoosePresentPage,
      meta:{requireAuth: true, module:['presentManager_boss'] }
    },
    {
      path: '/submitPresent/:type/:orderId',
      name: '提交赠送',
      component: SubmitPresentPage,
      meta:{requireAuth: true, module:['presentManager_boss'] }
    },
    {
      path: '/presentedList',
      name: '赠送酒水查询',
      component: PresentedListPage,
      meta:{requireAuth: true, module:['presentManager_boss'] }
    },
    {
      path: '/presentDetail/:presentId',
      name: '赠送酒水详情',
      component: PresentDetailPage,
      meta:{requireAuth: true, module:['presentManager_boss'] }
    },
    {
      path: '/presentStatistics',
      name: '赠送查询统计',
      component: PresentStatisticsPage,
      meta:{requireAuth: true, module:['presentManager_boss'] }
    },
    {
      path: '/turnoverStatistics',
      name: '营业额统计',
      component: TurnoverStatisticsPage,
      meta:{requireAuth: true, module:['statistics_boss'] }
    },
    {
      path: '/todaySelfHelpCustomer',
      name: '今日自来客',
      component: TodaySelfHelpCustomerPage,
      meta:{requireAuth: true, module:['statistics_boss'] }
    },
    {
      path: '/operatingRevenue',
      name: '总营业收入',
      component: OperatingRevenuePage,
      meta:{requireAuth: true, module:['statistics_boss'] }
    },
    {
      path: '/turnoverRatio',
      name: '翻台率',
      component: TurnoverRatioPage,
      meta:{requireAuth: true, module:['statistics_boss'] }
    },
    {
      path: '/openRatio',
      name: '开台率',
      component: OpenRatioPage,
      meta:{requireAuth: true, module:['statistics_boss'] }
    },
    {
      path: '/areaTimeStatistics',
      name: '区域时段统计',
      component: AreaTimeStatisticsPage,
      meta:{requireAuth: true, module:['statistics_boss'] }
    },
    {
      path: '/areaMinChargeStatistics',
      name: '区域低消统计',
      component: AreaMinChargeStatisticsPage,
      meta:{requireAuth: true, module:['statistics_boss'] }
    },
    {
      path: '/salesSummary',
      name: '酒水销售汇总',
      component: SalesSummaryPage,
      meta:{requireAuth: true, module:['statistics_boss'] }
    },
    {
      path: '/danceRevenue',
      name: '点舞收入',
      component: DanceRevenuePage,
      meta:{requireAuth: true, module:['statistics_boss'] }
    },
    {
      path: '/cardRevenue',
      name: '办卡/充值收入',
      component: CardRevenuePage,
      meta:{requireAuth: true, module:['statistics_boss'] }
    },
    {
      path: '/onAccountStatistics',
      name: '挂账统计',
      component: OnAccountStatisticsPage,
      meta:{requireAuth: true, module:['statistics_boss'] }
    },
    {
      path: '/presentStatisticsDetail',
      name: '赠送统计明细',
      component: PresentStatisticsDetailPage,
      meta:{requireAuth: true, module:['statistics_boss'] }
    },
    {
      path: '/damagedStatistics',
      name: '破损统计',
      component: DamagedStatisticsPage,
      meta:{requireAuth: true, module:['statistics_boss'] }
    },
    {
      path: '/stockData',
      name: '股东数据',
      component: StockDataPage,
      meta:{requireAuth: true, module:['statistics_boss'] }
    },
    {
      path: '/subscribePerformance',
      name: '订台业绩',
      component: SubscribePerformancePage,
      meta:{requireAuth: true, module:['performance_boss'] }
    },
    {
      path: '/drinksPerformance',
      name: '酒水业绩',
      component: DrinksPerformancePage,
      meta:{requireAuth: true, module:['performance_boss'] }
    },
    {
      path: '/orderPerformance',
      name: '点单业绩',
      component: OrderPerformancePage,
      meta:{requireAuth: true, module:['performance_boss'] }
    },
    {
      path: '/dancePerformance',
      name: '点舞业绩',
      component: DancePerformancePage,
      meta:{requireAuth: true, module:['performance_boss'] }
    },
    {
      path: '/drinksRanking',
      name: '酒水销售排行',
      component: DrinksRankingPage,
      meta:{requireAuth: true, module:['performance_boss'] }
    },
    {
      path: '/areaRanking',
      name: '区域排行',
      component: AreaRankingPage,
      meta:{requireAuth: true, module:['performance_boss'] }
    },
    {
      path: '/memberRanking',
      name: '会员排行',
      component: MemberRankingPage,
      meta:{requireAuth: true, module:['performance_boss'] }
    },
    {
      path: '/tableSalesRanking',
      name: '台位销售排行',
      component: TableSalesRankingPage,
      meta:{requireAuth: true, module:['performance_boss'] }
    },
    {
      path: '/tableSubscribeRanking',
      name: '台位订位排行',
      component: TableSubscribeRankingPage,
      meta:{requireAuth: true, module:['performance_boss'] }
    }
  ]
})

router.beforeEach((to, from, next) => {//验证
  //console.log(to, from)
  setTimeout(()=>{
    store.commit('updateLoadingStatus', {isLoading: true})

    function requireAuth(){
      return new Promise(function(resolve, reject){
        if(to.meta.requireAuth) resolve()
        else reject({path:'',mess:'无需授权登录的页面'})
      })
    }
    function loginOrNot(){
      return new Promise(function(resolve, reject){
        //console.log('router index:',sessionStorage.getItem('index'))
        if (getLocalStorage('token_boss_index' + sessionStorage.getItem('index')) && getLocalStorage('userId_boss_index' + sessionStorage.getItem('index'))) resolve()
        else reject({path:'/login',mess:'登陆信息失效，请重新登陆，或关闭页面后重新进入系统'})
      })
    }
    function getAuthority(){
      return new Promise(function(resolve, reject){
        if(JSON.stringify(store.getters.getAuthority) != '{}')
          resolve()
        else
          store.dispatch('fetchAuthority',{callback:(status)=>{
              if(status == 'success'){
                resolve()
              }else if(status == 'error'){
                reject({path:'/login',mess:'用户未设置任何权限'})
              }
            }
          })
      })
    }

    requireAuth().then((mess)=>{
      return loginOrNot()
    }).then((mess)=>{
      return getAuthority()
    }).then((mess)=>{
      next()
    }).catch((data)=>{
      console.log(data.mess)
      if(data.path == '/login'){
        delLocalStorage('token_boss_index' + sessionStorage.getItem('index'))
        delLocalStorage('userId_boss_index' + sessionStorage.getItem('index'))
        clearvuex()
        store.commit('updateLoadingStatus', {isLoading: false})
        message({message:data.mess,type:'error'})
        next({path:'/login'})
      }else{
        if(data.path == '')next()
        else next(data.path)
      }
    })
  },1)
});

router.afterEach((to, from) => {//验证
  /*
    store.commit('updateLoadingStatus', {isLoading: false})
  */
  if(from.path == '/login'){
  }
});

export default router
